/*
 * Copyright (c) 2011. c.mueller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package de.altitude.cimuelle.commandhandles;

import de.altitude.cimuelle.Config;
import de.altitude.cimuelle.coop.CoopDataHandler;
import de.altitude.cimuelle.db.DatabaseManager;
import de.altitudecustomcommands.Player;
import de.altitudecustomcommands.playerHandler;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * Class description:
 * processes structure destroyed or damaged events
 * User: cybernaut
 * Date: 07.10.11
 * Time: 22:40
 */
public class TypeStructureHandler {
    private final playerHandler playerList;
    private Config config;
    private static final Logger LOGGER = Logger.getLogger("TypeStructureHandler");

    public TypeStructureHandler(playerHandler playerList) {
        this.playerList = playerList;
        config = Config.getInstance();
    }

    public void typeStructureDestroy(Map json, String port, int playerId) {
        if (config.isDebug()) {
            LOGGER.debug(new StringBuilder()
                    .append(port)
                    .append(":Stucture ")
                    .append(json.get("target"))
                    .append(" destroyed by ")
                    .append(playerId)
                    .append("(")
                    .append(
                            playerList.getFirstPlayerByPortAndId(
                                    port,
                                    playerId).getName()
                    )
                    .append(")")
                    .toString());
        }
        if ("turret".equals(json.get("target"))) {
            // turret destroyed by player
            DatabaseManager.getInstance().addPointToPlayerForTurret(playerList.getPlayersByPortAndId(port, playerId));
            for (Player player : playerList.getPlayersByPortAndId(port, playerId)) {
                player.getStats().increaseDestroyedTurret();
                CoopDataHandler.updateGameStatsTurretKill(port, player);
            }
        } else if ("base".equals(json.get("target"))) {
            for (Player player : playerList.getPlayersByPortAndId(port, playerId)) {
                player.getStats().increaseDestroyedBases();
                CoopDataHandler.updateBaseDestroyed(port, player);
            }
        }

    }

    public void typeStructureDamage(Map json, String port, int playerId) {
        // {"port":28378,"time":206946340,"player":3,"target":"turret","xp":1,"type":"structureDamage"}
        String target = String.valueOf(json.get("target"));
        int damage = ((Number) json.get("xp")).intValue();
        if ("turret".equals(target)) {
            Player player = playerList.getFirstPlayerByPortAndId(port, playerId);
            player.getStats().increaseDamageDealtToTurrets(damage);
            CoopDataHandler.updateGameStatsTurretDamage(port, damage);
        }

    }
}
